/**
 * 
 */
package bud.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author zhangwq
 */
public abstract class DateUtils
{
    private static final String default_date_Pattern = "yyyy-MM-dd";
    private static final String default_time_Pattern = "HH:mm:ss";

    public static String getCurrentDate()
    {
        return getCurrentDate(default_date_Pattern);
    }

    public static String getCurrentDate(String pattern)
    {
        pattern =
            (pattern != null && !pattern.equals("")) ? pattern
                                                    : default_date_Pattern;
        return formatDate2String(pattern);
    }

    public static String getCurrentTime()
    {
        return getCurrentTime(default_time_Pattern);
    }

    public static String getCurrentTime(String pattern)
    {
        pattern =
            (pattern != null && !pattern.equals("")) ? pattern
                                                    : default_time_Pattern;
        return formatDate2String(pattern);
    }

    private static String formatDate2String(String pattern)
    {
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        Date today = new Date();
        return df.format(today);
    }

    public static Date getDate(String date)
    {
        SimpleDateFormat sdf = new SimpleDateFormat(default_date_Pattern);
        try
        {
            return sdf.parse(date);
        }catch (ParseException e)
        {
            return null;
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        System.out.println(DateUtils.getCurrentDate());
        System.out.println(DateUtils.getCurrentDate("MM-dd-yyyy"));
        System.out.println(DateUtils.getCurrentDate("MM/dd/yyyy"));
        System.out.println(DateUtils.getCurrentTime());
        System.out.println(DateUtils.getCurrentTime("HH=mm=ss"));
        System.out.println(DateUtils.getCurrentTime("HH-mm-ss"));
    }
}
